<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" append-to-body :close-on-click-modal="false" @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="服务商" prop="serviceCode">
            <el-select v-model="dataForm.serviceCode" clearable class="m-2" placeholder="服务商">
              <el-option v-for="item in dicts.lot_device_type" :key="item.value" :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编号" prop="sn">
            <el-input v-model="dataForm.sn" placeholder="设备编号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/roadStop/lotdevice'

export default {
  dicts: ['lot_device_type'],
  data () {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        type: '0',
        isOnLine: '0',
      },
      dataRule: {
        serviceCode: [
          { required: true, message: '服务code(适配不同商家地磁)不能为空', trigger: 'blur' }
        ],

        sn: [
          { required: true, message: '地磁编号不能为空', trigger: 'blur' }
        ],

        lotId: [
          { required: true, message: '泊位(车位id)编号不能为空', trigger: 'blur' }
        ],

        parkId: [
          { required: true, message: '车场id不能为空', trigger: 'blur' }
        ],

        type: [
          { required: true, message: '0未分配，1已分配不能为空', trigger: 'blur' }
        ],

        isOnLine: [
          { required: true, message: '是否在线不能为空', trigger: 'blur' }
        ],

      }
    }
  },
  methods: {
    init (id) {
      this.visible = true;
      this.canSubmit = true;
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;

          addObj(this.dataForm).then(data => {
            this.$notify.success('添加成功')
            this.visible = false
            this.$emit('refreshDataList')
          }).catch(() => {
            this.canSubmit = true;
          });

        }
      })
    },
    //重置表单
    closeDialog () {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
